package com.jeesite.modules.digitalize.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.jeesite.modules.digitalize.config.Constants;
import com.jeesite.modules.digitalize.constants.MessageConstants;
import com.jeesite.modules.digitalize.service.DigitalizeMessageService;
import com.jeesite.modules.digitalize.entity.DigitalizeTask;
import com.jeesite.modules.digitalize.entity.vo.DigitalizeTaskVO;
import com.jeesite.modules.digitalize.service.DigitalizeTaskService;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.jeesite.modules.digitalize.constants.MessageConstants.PROCESS_TASK_CONTENT;

@Component
@EnableScheduling
public class TaskMessageNoticeTask {

    @Resource
    DigitalizeTaskService digitalizeTaskService;
    @Resource
    DigitalizeMessageService digitalizeMessageService;
    //@Scheduled(cron="0/5 * *  * * ? ")   //每5秒执行一次
    //@Scheduled(cron="0 0 */1 * * ?")   //每1小时执行一次
    // 每周执行一次
    @Scheduled(cron = "0 0 0 ? * ? ")   //每天0点执行一次
    public void execute() {
        List<DigitalizeTaskVO> noticeList = digitalizeTaskService.selectWarnTask();
        if (CollectionUtil.isNotEmpty(noticeList)) {
            for (DigitalizeTaskVO digitalizeTaskVO : noticeList) {
                digitalizeMessageService.sendMsg(digitalizeTaskVO.getCurrentUserId(), MessageConstants.PROCESS_WARN_TASK, String.format(MessageConstants.PROCESS_WARN_TASK_CONTENT,  digitalizeTaskVO.getTaskName(),DateUtil.format(digitalizeTaskVO.getEndDate(), DatePattern.NORM_DATE_PATTERN)), digitalizeTaskVO.getProjectId());
            }
            List<Long> idList = noticeList.stream().map(DigitalizeTask::getId).collect(Collectors.toList());
            digitalizeTaskService.lambdaUpdate().set(DigitalizeTask::getNoticeStatus, Constants.YES).in(DigitalizeTask::getId, idList).update();
        }
    }
}
